/************************************************
 ** Copyright (C) 2000-2015 TIZA Inc..
 ** 类    名：picFactory
 ** 作    者：hlq
 ** 描    述：图片处理工厂类
 ** 生成日期：2015/4/27
 ** 修改日志：
 *************************************************/
function picFactory() {}
picFactory.prototype = {
	getPhoto: function(callback) {
		var that = this;
		plus.nativeUI.actionSheet({
			title: "选择照片",
			cancel: "取消",
			buttons: [{
				title: "相册"
			}, {
				title: "拍照"
			}]
		}, function(e) {
			var index = e.index;
			switch (index) {
				case 0:
					break;
				case 1:
					plus.gallery.pick(function(entry) {
						plus.nativeUI.showWaiting();
						that.compress(entry, function(url) {
							plus.nativeUI.closeWaiting();
							callback(url);
						});
					}, function(error) {
						//mui.toast(error.message);
					});
					break;
				case 2:
					var cmr = plus.camera.getCamera();
					cmr.captureImage(function(p) {
						plus.io.resolveLocalFileSystemURL(p, function(entry) {
							plus.nativeUI.showWaiting();
							that.compress(entry.toLocalURL(), function(url) {
								plus.nativeUI.closeWaiting();
								callback(url);
							});
						});
					}, function(error) {
						//mui.toast(error.message);
					});
					break;
			}
		});
	},
	//压缩
	compress: function(path, callback) {
		var img = new Image();
		img.src = path;
		img.onload = function() {					
			var that = this;
			//生成比例
			var w = that.width,
				h = that.height,
				scale = w / h,
				w = 500 || w,
				h = w / scale;
			//生成canvas
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			canvas.setAttribute("width", w);
			canvas.setAttribute("height", h);
			ctx.drawImage(that, 0, 0, w, h);
			var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8);
			callback(base64);
		};
	},
	//转换成base64
	convertToBase64: function(path, callback) {
		var img = new Image();
		img.src = path;
		img.onload = function() {
			var that = this;
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			canvas.setAttribute("width", that.width);
			canvas.setAttribute("height", that.height);
			ctx.drawImage(that, 0, 0, that.width, that.height);
			var base64 = canvas.toDataURL('image/jpeg', 1);
			callback(base64);
		};
	}
};
